<?php

namespace app\admin\controller;

use app\admin\model\PohtoReport as PohtoReportModel;
use app\admin\validate\CommonnValidate;
use app\BaseController;

class PhotoReport extends BaseController
{
    protected $middleware = ['Auth'];
    public function list() {
        $params = $this->request->post(['current', 'size', 'photo_id', 'user_id', 'title', 'status', 'query']);
        $validate = new CommonnValidate();
        if (!$validate->scene('list')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $sql = new PohtoReportModel();
        $whereArr = [];
        $haswhereArr = [];
        if(isset($params['title']) && $params['title']) {
            $whereArr[] = ["title", "=", $params['title']];
        }
        if(isset($params['photo_id']) && $params['photo_id']) {
            $whereArr[] = ["photo_id", "=", $params['photo_id']];
        }
        if(isset($params['user_id']) && $params['user_id']) {
            $whereArr[] = ["user_id", "=", $params['user_id']];
        }
        if(isset($params['status']) && $params['status']) {
            $whereArr[] = ["status", "=", $params['status']];
        }
        $condition = isset($params['query']) && $params['query'];
        if($condition) {
            $haswhereArr[] = ["name", "LIKE", '%'.$params['query'].'%'];
        }
        $list = $sql
            ->where($whereArr)
            ->when($condition, function ($query) use($haswhereArr) {
                $query->hasWhere('photo', $haswhereArr);
            })
            ->with(['photo', 'user'])
            ->order('create_at desc')
            ->paginate([
                'list_rows'=> request()->param('size'),
                'page' => request()->param('current'),
            ]);
        return result()->success($list);
    }

    public function status() {
        $params = $this->request->post(['id', 'status']);
        $sql = new PohtoReportModel();
        $sql->update([
            'status' => $params['status']
        ], ['id' => $params['id']]);
        return result()->success("修改成功");
    }
}